<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class TrajectoryController extends Controller
{
    public function getTrajectoryList(){

        try{
            $results = DB::table('aboutcompany')->where('type','=','5')->orderby('time','asc')->paginate(10);
//            dd($results);

            return view('Admin.trajectorylist',['results'=> $results]);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function addTrajectory(){

        try{
            $data = Request::only('time', 'editorValue');

            $validator = Validator::make($data, [
                'time' => 'required|digits:4',
                'editorValue' => 'required',
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }

            $d['time'] = $data['time'];
            $d['aboutcompany_content'] = $data['editorValue'];
            $d['type']=5;
            $result = DB::table('aboutcompany')->insert($d);
            if($result){

                return redirect('/admin/getTrajectoryList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function updateTrajectory(){

        try{
            $data = Request::only('aboutcompany_id','time', 'editorValue');

            $validator = Validator::make($data, [
                'aboutcompany_id' => 'required',
                'time' => 'required|digits:4',
                'editorValue' => 'required',
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }
            $d['time'] = $data['time'];
            $d['aboutcompany_content'] = $data['editorValue'];
            $result = DB::table('aboutcompany')->where('aboutcompany_id','=',$data['aboutcompany_id'])->update($d);
            if($result){

                return redirect('/admin/getTrajectoryList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function deleteTrajectory(){

        try{
            $data = Request::only('aboutcompany_id');
            $results = DB::table('aboutcompany')->where('aboutcompany_id','=',$data['aboutcompany_id'])->delete();
            if($results){

                return redirect('/admin/getTrajectoryList');
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }

    }
}
